
balance_plot_data <-
  bind_rows(data_1, data_2) %>%
  mutate(islam = (RELIGION == 1)*1,
         christian = (RELIGION == 2)*1,
         catholic = (RELIGION == 3)*1,
         male = (GENDER == 1)*1,
         female = (GENDER == 2)*1,
         district_gov = (instansi_category == "district")*1,
         province_gov = (instansi_category == "province")*1,
         central_gov = (instansi_category == "central")*1,
         javanese = (q5_survey_ethnic == "Jawa")*1,
         sundanese = (q5_survey_ethnic == "Sunda")*1,
         malay = (q5_survey_ethnic == "Melayu")*1,
         minang = (q5_survey_ethnic == "Minang")*1,
         other_eth = (!(q5_survey_ethnic %in% c("Jawa", "Sunda", "Melayu", "Minang")))*1)

vars <- c("islam", "christian", "catholic", "male", "female", "AGE", "district_gov", "province_gov", "central_gov", "javanese", "sundanese", "malay", "minang", "other_eth")


make_pval = function(var_name){
  
  form1 = as.formula(paste0(var_name, "~ pass_final"))
  reg_df1 = 
    balance_plot_data %>% 
    filter(abs(forcing_final) < 1)
  
  pval1 =
    lm_robust(formula = form1, data = reg_df1) %>%
    tidy() %>%
    .[2,5]
  
  form2 = as.formula(paste0(var_name, "~ fail_skd"))
  reg_df2 = 
    balance_plot_data %>% 
    filter(abs(forcing_skd) < 5)
  
  pval2 =
    lm_robust(formula = form2, data = reg_df2) %>%
    tidy() %>%
    .[2,5]
  
  data.frame(pval = c(pval1, pval2), cutpoint = c("pass_final", "fail_skd"), outcome = var_name)
  
}

plot_df = lapply(vars, make_pval) %>% bind_rows()


plot_dat <-
  plot_df %>%
  mutate(outcome = case_when(outcome == "islam" ~ "Islam",
                             outcome == "christian" ~ "Christian",
                             outcome == "catholic" ~ "Catholic",
                             outcome == "male" ~ "Male",
                             outcome == "female" ~ "Female",
                             outcome == "AGE" ~ "Age",
                             outcome == "district_gov" ~ "District",
                             outcome == "province_gov" ~ "Province",
                             outcome == "central_gov" ~ "Central",
                             outcome == "javanese" ~ "Javanese",
                             outcome == "sundanese" ~ "Sundanese",
                             outcome == "malay" ~ "Malay",
                             outcome == "minang" ~ "Minang",
                             outcome == "other_eth" ~ "Other")) %>%
  
  add_row(outcome = "Religion:                ", pval = 5, cutpoint = NA_character_) %>%
  add_row(outcome = "Demographics:            ", pval = 5, cutpoint = NA_character_) %>%
  add_row(outcome = "Level:                   ", pval = 5, cutpoint = NA_character_) %>%
  add_row(outcome = "Ethnicity:               ", pval = 5, cutpoint = NA_character_) %>%
  mutate(outcome = factor(outcome, levels = c("Religion:                ", "Islam", "Christian", "Catholic",
                                              "Demographics:            ", "Female", "Male", "Age",
                                              "Level:                   ", "District", "Province", "Central",
                                              "Ethnicity:               ", "Javanese", "Sundanese", "Malay", "Minang", "Other"))) %>%
  mutate(cutpoint = case_when(cutpoint == "pass_final" ~ "Integrated",
                              cutpoint == "fail_skd" ~ "Basic"))

balance_plot <-
  plot_dat %>%
  ggplot(aes(x= pval, y = fct_rev(outcome), color = cutpoint)) +
  geom_point(stroke = 1) +
  geom_vline(xintercept = 0.05, linetype = "dashed") +
  xlim(0, 1) +
  theme_bw() +
  xlab("p-value") +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #axis.line.y.left = element_blank(),
        #legend.position = "none",
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        axis.title.y = element_blank(),
        legend.background = element_rect(colour = "black")) +
  scale_colour_discrete(na.translate = F) +
  theme(text=element_text(size=10))


ggsave(filename = "./_4_outputs/figures/figure_a2.png", plot = balance_plot, width = 6, height = 4)




